// pages/custom/custom.ts
import { MerchantAPI } from '../../utils/api'

interface CustomRequirement {
  requirementId: string;
  title: string;
  description: string;
  votes: number;
  rank: number;
  status: string;
  userPreferences: {
    ageGroup: string;
    gender: string;
    purchaseHabits: string;
  };
  periodRemainingDays: number;
  deadlineHours: number;
  createdAt: string;
  estimatedCompletionTime?: string;
}

Page({
  data: {
    loading: false,
    refreshing: false,
    stats: {
      totalRequirements: 0,
      pendingCount: 0,
      acceptedCount: 0
    },
    customRequirements: [] as CustomRequirement[],
    
    // 接单功能
    showAcceptModal: false,
    acceptingRequirement: null as CustomRequirement | null,
    estimatedTime: ''
  },

  onLoad() {
    this.loadCustomRequirements();
  },

  async loadCustomRequirements() {
    this.setData({ loading: true });

    try {
      // 调用后端API：/workbench/custom/requirements
      // const response = await MerchantAPI.getCustomRequirements();

      // 模拟数据
      const mockRequirements: CustomRequirement[] = [
        {
          requirementId: "NO.20240501",
          title: "低糖少冰珍珠奶茶（三分糖）",
          description: "想要低糖版本的珍珠奶茶，三分糖，冰量可选，珍珠要Q弹不粘牙",
          votes: 328,
          rank: 3,
          status: "pending",
          userPreferences: {
            ageGroup: "18-25岁占70%",
            gender: "女性65%", 
            purchaseHabits: "常购奶茶类"
          },
          periodRemainingDays: 7,
          deadlineHours: 72,
          createdAt: "2024-05-01 10:00:00"
        }
      ];

      this.setData({
        customRequirements: mockRequirements,
        stats: {
          totalRequirements: mockRequirements.length,
          pendingCount: mockRequirements.filter(r => r.status === 'pending').length,
          acceptedCount: mockRequirements.filter(r => r.status === 'accepted').length
        }
      });

    } catch (error) {
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  acceptRequirement(e: any) {
    const requirement = e.currentTarget.dataset.requirement;
    this.setData({
      showAcceptModal: true,
      acceptingRequirement: requirement
    });
  },

  async confirmAccept() {
    const { acceptingRequirement, estimatedTime } = this.data;
    
    this.setData({ loading: true });

    try {
      // 调用后端API：/workbench/custom/accept
      const userInfo = wx.getStorageSync('merchantUserInfo');
      // await MerchantAPI.acceptCustomRequirement({
      //   requirementId: acceptingRequirement!.requirementId,
      //   merchantId: userInfo.merchantId,
      //   estimatedCompletionTime: estimatedTime,
      //   operatorId: userInfo.userId
      // });

      wx.showToast({
        title: '接单成功',
        icon: 'success'
      });

      this.setData({
        showAcceptModal: false
      });

      this.loadCustomRequirements();

    } catch (error) {
      wx.showToast({
        title: '接单失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  onEstimatedTimeChange(e: any) {
    this.setData({
      estimatedTime: e.detail.value
    });
  },

  viewRequirementDetail(e: any) {
    wx.showToast({
      title: '需求详情功能开发中',
      icon: 'none'
    });
  },

  onRefresh() {
    this.setData({ refreshing: true });
    this.loadCustomRequirements();
    setTimeout(() => {
      this.setData({ refreshing: false });
    }, 1000);
  },

  closeAcceptModal() {
    this.setData({
      showAcceptModal: false
    });
  },

  preventClose() {}
});









